home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / ASM 2.0 ƒ / lookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  3.0 KB  |  156 lines

  1. #include <stdio.h>
  2. #include "as.h"
  3. #include "table.h"
  4. #include "lookup.h"
  5.  
  6. extern int Pass;
  7. extern int Debug;
  8.  
  9. #define HASHSIZE 203                            /* width of hash table */
  10. static struct nlist *hashtab[HASHSIZE] = {0};   /* symbol table */
  11.  
  12. /*
  13.  *      hash --- form hash value for string s
  14.  */
  15. hash(s)
  16. register char *s;
  17. {
  18.     register int hashval = 0;
  19.  
  20.     while(*s)
  21.         hashval += *s++;
  22.     return(hashval % HASHSIZE );
  23. }
  24.  
  25. /*
  26.  *      install --- add a symbol to the table
  27.  */
  28. install(str,val,type)
  29. char    *str;
  30. int     val;
  31. char    type;
  32. {
  33.     register struct nlist *np;
  34.     int     i;
  35.     char *malloc();
  36.     extern char *tnames[];
  37.  
  38.     if( !alpha(*str) ){
  39.         serror("Illegal Symbol Name: %s",str);
  40.         return;
  41.         }
  42.     if( (np = lookup(str)) != NULL ){
  43.         if( Pass==2 ){
  44.             if( np->sym_def == val && np->sym_type == type )
  45.                 return;
  46.             else{
  47.                 error("Phasing Error");
  48.                 return;
  49.                 }
  50.             }
  51.         if( np->sym_type != SYM )
  52.             serror("Reserved Symbol Name: %s",str);
  53.         else
  54.             serror("Symbol Redefined: %s",str);
  55.         return;
  56.         }
  57.  
  58. if(Debug&LOOKUP)printf("Install: %s=0x%x(%s)\n",str,val,tnames[type]);
  59.  
  60.     /* enter new symbol */
  61.     np = (struct nlist *) malloc(sizeof(struct nlist));
  62.     if( np == (struct nlist *)ERR ){
  63.         error("Symbol table full");
  64.         return;
  65.         }
  66.     np->sym_name = malloc(strlen(str)+1);
  67.     if( np->sym_name == (char *)ERR ){
  68.         error("Symbol table full");
  69.         return;
  70.         }
  71.     strcpy(np->sym_name,str);
  72.     np->sym_def  = val;
  73.     np->sym_type = type;
  74.     i = hash(np->sym_name);
  75.     np->sym_next = hashtab[i];
  76.     hashtab[i] = np;
  77.     return;
  78. }
  79.  
  80. /*
  81.  *      lookup --- find string in symbol table
  82.  */
  83. struct nlist *
  84. lookup(name)
  85. register char *name;
  86. {
  87.     register struct nlist *np;
  88.  
  89.     for( np = hashtab[hash(name)] ; np != NULL ; np = np->sym_next)
  90.         if( strcmp(name,np->sym_name)==0 )
  91.             return(np);
  92.     if(Pass==2)
  93.         serror("Undefined: %s",name);
  94.     return(NULL);
  95. }
  96.  
  97. /*
  98.  *      mne_look --- mnemonic lookup
  99.  *
  100.  *      Return pointer to an mne structure if found.
  101.  */
  102. struct mne *
  103. mne_look(str)
  104. char    *str;
  105. {
  106.     register struct mne *low,*high,*mid;
  107.     int     cond;
  108.     extern struct mne mnemonic[];
  109.     extern int Nmne;
  110.  
  111.     low =  &mnemonic[0];
  112.     high = &mnemonic[ Nmne-1 ]; /* last entry in table is always empty */
  113.     while (low <= high){
  114.         mid = low + (high-low)/2;
  115.         if( ( cond = strcmp(str,mid->mne_name)) < 0)
  116.             high = mid - 1;
  117.         else if (cond > 0)
  118.             low = mid + 1;
  119.         else
  120.             return(mid);
  121.     }
  122.     return(NULL);
  123. }
  124.  
  125. /*
  126.  *      dump_syms --- print statistics about symbol table usage
  127.  */
  128. dump_syms()
  129. {
  130.     register int i;
  131.     register int total;
  132.     register int gtotal = 0;
  133.     register struct nlist *s;
  134.     int     minlist = 9999999;
  135.     int     maxlist = 0;
  136.  
  137.  
  138.     printf("Symbol table distribution\n");
  139.     for(i=0;i<HASHSIZE;i++){
  140.         total = 0;
  141.         for(s = hashtab[i]; s != NULL; s=s->sym_next)
  142.             total++;
  143.         printf("%d ",total);
  144.         if( total > maxlist )
  145.             maxlist = total;
  146.         if( total < minlist )
  147.             minlist = total;
  148.         gtotal += total;
  149.         if( (i%20) == 19 )
  150.             printf("\n");
  151.         }
  152.     printf("\nTotal symbols: %d\n",gtotal);
  153.     printf("Minimum list length: %d\n",minlist);
  154.     printf("Maximum list length: %d\n",maxlist);
  155. }
  156.